﻿@model PagedList.IPagedList<EasyHome.Models.TablaGenerica>
@using PagedList.Mvc;

<!DOCTYPE html>

@{
    ViewBag.Title = "Tablas Genericas";
}

<h2>Tabla Generica</h2>
@using (Html.BeginForm("Index", "TablaGenerica", FormMethod.Get, new { @class = "form-inline" }))
{
    <div class="form-group">
        <h5>Descripción:</h5>
    </div>
    <div class="form-group">
        @Html.TextBox("SearchString", ViewBag.CurrentFilter as string, new { @class = "form-control" })
    </div>
    <div class="form-group">
        <button class="btn btn-default" type="submit">Search</button>
    </div>
    <div class="form-group">
        @Html.ActionLink("Create", "Create", null, null, new { id = "btnCreate", @class = "btn btn-info pull-right" })
    </div>
}
<br />
<table class="table">
    <tr>
        <th>
            @Html.ActionLink("Descripcion", "Index", new { sortOrder = ViewBag.DescripcionSort, currentFilter = ViewBag.CurrentFilter })
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Descripcion)
            </td>
            <td>
                @Html.ActionLink(" ", "Edit", new { id = item.id }, new { @class = "glyphicon glyphicon-pencil" })
                @Html.ActionLink(" ", "Details", new { id = item.id }, new { @class = "glyphicon glyphicon-search" })
                @Html.ActionLink(" ", "Delete", new { id = item.id }, new { @class = "glyphicon glyphicon-trash" })
            </td>
        </tr>
    }

</table>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

@Html.PagedListPager(Model, page => Url.Action("Index",
    new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

<div id='dialogDiv' class='modal fade'>
    <div class="modal-dialog">
        <div class="modal-content" id='dialogContent'></div>
    </div>
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

    <script type="text/javascript">
        $(function () {
            //Optional: turn the chache off
            $.ajaxSetup({ cache: false });
            /*‒‒||‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒||‒‒*/
            $('#btnCreate, .glyphicon-search, .glyphicon-trash').click(function () {
                $('#dialogContent').load(this.href, function () {
                    $('#dialogDiv').modal({
                        backdrop: 'static',
                        keyboard: true
                    }, 'show');
                    bindForm(this);
                });
                return false;
            });
        });
        /*‒‒||‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒||‒‒*/
        function bindForm(dialog) {
            $('form', dialog).submit(function () {
                $.ajax({
                    url: this.action,
                    type: this.method,
                    data: $(this).serialize(),
                    success: function (result) {
                        if (result.success) {
                            $('#dialogDiv').modal('hide');
                            // Refresh:
                            location.reload();
                        } else {
                            $('#dialogContent').html(result);
                            bindForm();
                        }
                    }
                });
                return false;
            });
        }
        /*‒‒||‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒||‒‒*/
    </script>
}